package com.laxcen.smoke.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.laxcen.smoke.dao.AbNormalDao;
import com.laxcen.smoke.dao.IntelligentMonitorDao;
import com.laxcen.smoke.entity.AbNormalRecord;
import com.laxcen.smoke.service.AbNormalService;
import com.laxcen.smoke.util.DBUtils;
import com.laxcen.smoke.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Service
public class AbNormalServiceImpl implements AbNormalService {

    @Autowired
    private AbNormalDao abNormalDao;

    @Autowired
    private IntelligentMonitorDao intelligentMonitorDao;

    @Override
    public Response insert(String smartno,String sboxno, Integer abnormaltype) {

        Date date = new Date();
        //添加到smart表
        long ids = DBUtils.nextId();
        String id = String.valueOf(ids);
        abNormalDao.insert(id,smartno,sboxno,abnormaltype,date,1);
        Response response = new Response();
        response.setOK();
        return response;
    }

    @Override
    public Response update(String id,String username,int status,String content) {
//        if (status==0) {
//            //修改格子表格子状态
//            List<String> listboxid = abNormalDao.getsmartboxid(abNormalRecord.getSmartno(), abNormalRecord.getSmartboxno());
//            for (int i = 0; i < listboxid.size();i++) {
//                abNormalDao.updateBoxStatus(1, listboxid.get(i));
//            }
//
//            //修改智能柜表状态信息
//            //修改实体柜表状态信息
//            abNormalDao.updatesmartboard(abNormalDao.getsmartboardid(abNormalRecord.getSmartno()));
//            List<String> abNormalRecords = intelligentMonitorDao.getAbn();
//            for (int i = 0; i < abNormalRecords.size();i++) {
//                //TODO 查询主副柜、格子表是否有异常
//                List<String> list = intelligentMonitorDao.getNotState(abNormalRecords.get(i), 3);
//                if (list.size() == 0) {
//                    //修改智能柜表状态信息
//                    intelligentMonitorDao.updateSmartByNo(0, abNormalRecords.get(i));
//                    //修改主柜状态信息
//                    intelligentMonitorDao.updateSmartBoardByNo(0,abNormalRecords.get(i));
//                    List<String> idlist = intelligentMonitorDao.getIdBySmartno(abNormalRecords.get(i),3);
//                    for (int j = 0; j < idlist.size(); j++) {
//                        //修改实体柜表状态信息
//                        intelligentMonitorDao.updatesmartBoardBy(0, abNormalRecords.get(i), idlist.get(j));
//                    }
//                }
//            }
//            List<AbNormalRecord> list = abNormalDao.getNotState(abNormalRecord.getSmartno(),1);
//            if (list.size()==0) {
//                abNormalDao.updateSmartByNo(0, abNormalRecord.getSmartno());
//                List<String> idlist = abNormalDao.getIdBySmartno(abNormalRecord.getSmartno());
//                for (int i = 0;i < idlist.size();i++) {
//                    abNormalDao.updatesmartBoardBy(0, abNormalRecord.getSmartno(), idlist.get(i));
//                }
//            }
        AbNormalRecord abNormalRecord = abNormalDao.getAbnById(id);
        //修改异常表状态信息
            abNormalDao.update(id, status, content, new Date());
            //添加到操作记录表
        Date date = new Date();
        long opids = DBUtils.nextId();
        String realopid = String.valueOf(opids);
        String orderno = null;
        if (abNormalRecord.getAbnormaltype()==4||abNormalRecord.getAbnormaltype()==5){
            orderno=abNormalRecord.getContent();
        }
        abNormalDao.insertuporder(realopid,abNormalRecord.getSmartno(),abNormalRecord.getSmartboxno(),username,date,3,orderno,date);
            //添加到操作记录详情表
        abNormalDao.insertuporderdetail(String.valueOf(DBUtils.nextId()),realopid,abNormalRecord.getSmartno(),abNormalRecord.getSmartboxno(),date,3,date);
//        }else if (status==1){
            //修改异常表状态信息
//            abNormalDao.update(id, status, content, new Date());
//        }
        Response response = new Response();
        response.setOK();
        return response;
    }

    @Override
    public Response<PageInfo<AbNormalRecord>> getList(String smartno, String starttime, String endtime, int roleflag, String province, String city, String area, int pageNo, int pageSize, int state) throws ParseException {
        Page page = PageHelper.startPage(pageNo, pageSize);
       List<AbNormalRecord> list = new LinkedList<>();
        if (roleflag==0) {
            if (1 == state || "1".equals(String.valueOf(state))) {
                list = abNormalDao.getListAllByState(1);
            }
            if ("".equals(smartno) && "".equals(starttime) && "".equals(endtime)) {
                list = abNormalDao.getListAll();
            } else if ("".equals(smartno) && !"".equals(starttime) && !"".equals(endtime)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date datestart = sdf.parse(starttime);
                Date dateend = sdf.parse(endtime);
                list = abNormalDao.getListByTime(datestart, dateend);
            } else if (!"".equals(smartno) && "".equals(starttime) && "".equals(endtime)) {
                list = abNormalDao.getListByNo(smartno);
            } else if (smartno == null && starttime == null && endtime == null) {
                list = abNormalDao.getListAll();
            } else if (!"".equals(smartno) && !"".equals(starttime) && !"".equals(endtime)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date datestart = sdf.parse(starttime);
                Date dateend = sdf.parse(endtime);
                list = abNormalDao.getListByTimeAndNo(smartno, datestart, dateend);
            } else if (smartno == null && starttime != null && endtime != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date datestart = sdf.parse(starttime);
                Date dateend = sdf.parse(endtime);
                list = abNormalDao.getListByTime(datestart, dateend);
            } else if (smartno != null && starttime == null && endtime == null) {
                list = abNormalDao.getListByNo(smartno);
            } else if (smartno != null && starttime != null && endtime != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date datestart = sdf.parse(starttime);
                Date dateend = sdf.parse(endtime);
                list = abNormalDao.getListByTimeAndNo(smartno, datestart, dateend);
            }
        }else if (roleflag==1) {
            String place = null;
            if (!province.isEmpty()){
                place = province;
            }
            if (!city.isEmpty()){
                place =place+"/"+ city;
            }
            if (!area.isEmpty()){
                place = place+"/"+area;
            }
            if (1 == state || "1".equals(String.valueOf(state))) {
                list = abNormalDao.getListAllByStateRole(1,place);
            }
            if ("".equals(smartno) && "".equals(starttime) && "".equals(endtime)) {
                list = abNormalDao.getListAllRole(place);
            } else if ("".equals(smartno) && !"".equals(starttime) && !"".equals(endtime)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date datestart = sdf.parse(starttime);
                Date dateend = sdf.parse(endtime);
                list = abNormalDao.getListByTimeRole(datestart, dateend,place);
            } else if (!"".equals(smartno) && "".equals(starttime) && "".equals(endtime)) {
                list = abNormalDao.getListByNoRole(smartno,place);
            } else if (smartno == null && starttime == null && endtime == null) {
                list = abNormalDao.getListAllRole(place);
            } else if (!"".equals(smartno) && !"".equals(starttime) && !"".equals(endtime)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date datestart = sdf.parse(starttime);
                Date dateend = sdf.parse(endtime);
                list = abNormalDao.getListByTimeandNoRole(smartno, datestart, dateend,place);
            } else if (smartno == null && starttime != null && endtime != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date datestart = sdf.parse(starttime);
                Date dateend = sdf.parse(endtime);
                list = abNormalDao.getListByTimeRole(datestart, dateend,place);
            } else if (smartno != null && starttime == null && endtime == null) {
                list = abNormalDao.getListByNoRole(smartno,place);
            } else if (smartno != null && starttime != null && endtime != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date datestart = sdf.parse(starttime);
                Date dateend = sdf.parse(endtime);
                list = abNormalDao.getListByTimeandNoRole(smartno, datestart, dateend,place);
            }
        }
        Response<PageInfo<AbNormalRecord>> response = new Response<>();
        PageInfo pageInfo = page.toPageInfo();
        pageInfo.setList(list);
        response.setOK();
        response.setData(pageInfo);
        return response;
    }

    @Override
    public Response<PageInfo<AbNormalRecord>> getAllList(int pageNo, int pageSize, int roleflag, String province, String city, String area) {
        List<AbNormalRecord> list = new LinkedList<>();
        Page page = PageHelper.startPage(pageNo, pageSize);
        if (roleflag==0) {
            List<AbNormalRecord> listall = abNormalDao.getListAll();
            int number = abNormalDao.getCount();
            for (int i = 0; i<listall.size();i++){
                list.add(i,listall.get(i));
                for (int j = 0; j < list.size(); j++) {
                    list.get(j).setNumber(number);
                }
            }

        }else if (roleflag==1){
            String place = null;
            if (!province.isEmpty()){
                place = province;
            }
            if (!city.isEmpty()){
                place =place+"/"+ city;
            }
            if (!area.isEmpty()){
                place = place+"/"+area;
            }
            List<AbNormalRecord> listall = abNormalDao.getListAllRole(place);
            int number = abNormalDao.getCountRole(place);
            for (int i = 0; i<listall.size();i++){
                list.add(i,listall.get(i));
                for (int j = 0; j < list.size(); j++) {
                    list.get(j).setNumber(number);
                }
            }
        }
        Response<PageInfo<AbNormalRecord>> response = new Response<>();
        PageInfo pageInfo = page.toPageInfo();
        pageInfo.setList(list);
        response.setData(pageInfo);
        response.setOK();
        return response;
    }

    @Override
    public Response<List<AbNormalRecord>> getOne(String smartno) {
        List<AbNormalRecord> list = abNormalDao.getListByNo(smartno);
        Response<List<AbNormalRecord>> response = new Response<>();
        response.setOK();
        response.setData(list);
        return response;
    }
}
